package cn.blunce.AutoTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Experiment on 2016/3/29 0029.
 *
 * data in file: xxx;xxx
 */
abstract public class SolutionTest {
    private List<Object> inputs;
    private List<Object> outputs;

    SolutionTest() {
        inputs = new ArrayList<Object>();
        outputs = new ArrayList<Object>();
        List<String> strings = readFileByLines("resource/test");//Relative path is the root of the project.

        for (String string : strings) {
            String[] line = string.split(";");
            inputs.add(inputParse(line[0]));
            outputs.add(outputParse(line[1]));
        }
    }

    /**
     * Parse the input in the file
     *
     * @param input:it has been split with ";".
     * @return
     */
    protected abstract Object inputParse(String input);

    /**
     * Parse the output in the file
     *
     * @param output: it has been split with ";"
     * @return
     */
    protected abstract Object outputParse(String output);

    /**
     * Parse the input in the solution to String
     *
     * @param input
     * @return
     */
    protected abstract String inputAntiParse(Object input);

    /**
     * Parse the output in the solution to String
     *
     * @param output
     * @return
     */
    protected abstract String outputAntiParse(Object output);

    /**
     * @param s
     * @param input
     * @return
     */
    protected abstract Object oneSolution(Solution s, Object input);

    /**
     * whether the result is right
     *
     * @param predict
     * @param result
     * @return
     */
    protected abstract boolean isRight(Object predict, Object result);

    public void Test(Solution s) {
        int i;
        Object predict = null;
        for (i = 0; i < inputs.size(); i++) {
            predict = oneSolution(s, inputs.get(i));
            if (!isRight(predict, outputs.get(i)))
                break;
        }
        if (i == inputs.size())
            System.out.println("Accept!!");
        else {
            System.out.println("Input:\t\t\t\t" + inputAntiParse(inputs.get(i)));
            System.out.println("Expected Output:\t" + outputAntiParse(outputs.get(i)));
            System.out.println("Output:\t\t\t\t" + outputAntiParse(predict));
        }
    }

    private List<String> readFileByLines(String filename) {
        List<String> strings = new ArrayList<String>();
        File file = new File(filename);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tmpString = null;
            while ((tmpString = reader.readLine()) != null)
                strings.add(tmpString);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strings;
    }
}
